<template>
<div class="app-container">
  <div class="filter-container">
    <el-button class="filter-item" style="margin-left: 10px; margin-bottom: 20px" type="primary" icon="el-icon-edit" @click="handleCreate">
      添加类型
    </el-button>
  </div>

  <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
    <el-table-column label="序号" prop="id" align="center" width="60">
      <template slot-scope="{ row }">
        <span>{{ row.id }}</span>
      </template>
    </el-table-column>
    <el-table-column label="类型名称" prop="type_name" align="center">
      <template slot-scope="{ row }">
        <span>{{ row.type_name }}</span>
      </template>
    </el-table-column>

    <el-table-column label="更新时间" prop="updateTime" align="center">
      <template slot-scope="{ row }">
        <span>{{ row.update_time }}</span>
      </template>
    </el-table-column>
    <el-table-column label="创建时间" prop="createTime" align="center">
      <template slot-scope="{ row }">
        <span>{{ row.create_time }}</span>
      </template>
    </el-table-column>

    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
      <template slot-scope="{ row }">
        <el-button type="primary" size="mini" @click="handleUpdate(row)">
          编辑
        </el-button>
        <el-button v-if="row.status != 'deleted'" size="mini" type="danger" @click="handleDelete(row)">
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="600px">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="70px" style="width: 400px; margin-left: 50px">
      <el-form-item label="类型名称">
        <el-input v-model="temp.type_name" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false"> 取消 </el-button>
      <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">
        确定
      </el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
import {
  fetchType,
  createAdertType,
  deleteBanner
} from '@/api/banner'

export default {
  name: 'Index',
  components: {},
  data() {
    return {
      tableKey: 0,
      list: null,
      listLoading: true,
      downloadLoading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      temp: {
        id: undefined,
        type_name: '',
        status: 1
      },
      textMap: {
        update: '编辑',
        create: '新增'
      },
      rules: {
        type_name: [{
          required: true,
          message: 'title is required',
          trigger: 'blur'
        }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchType().then((response) => {
        this.list = response.data.list
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      })
    },

    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createAdertType(this.temp).then((response) => {
            if (response.code === 0) {
              this.dialogFormVisible = false
              this.$message({
                type: 'success',
                message: '添加成功'
              })
              this.getList()
            } else {
              this.$message({
                type: 'error',
                message: response.message
              })
            }
          })
        }
      })
    },

    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.temp.id = row.id
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.createData()
        }
      })
    },

    deleteData(param) {
      deleteBanner(param).then((response) => {
        if (response.code === 0) {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        } else {
          this.$message({
            type: 'error',
            message: response.message
          })
        }
      })
    },

    handleDelete(row) {
      const data = {
        id: row.id
      }
      this.$confirm('此操作将会删除这条记录，是否继续?', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          this.deleteData(data)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    resetTemp() {
      this.temp = {
        id: undefined,
        startTime: new Date(),
        endTime: new Date(),
        type_name: '',
        status: 1
      }
    }
  }
}
</script>

<style lang="scss">
.item-content {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
}
</style>
